package com.example.smartcity.Service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.Comment;
import com.example.smartcity.Entity.User;
import com.example.smartcity.Mappers.NewsCommentMapper;
import com.example.smartcity.Mappers.UserMapper;
import com.example.smartcity.Mappers.VideoCommentMapper;
import com.example.smartcity.Service.NewsCommentService;
import com.example.smartcity.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import sun.util.calendar.BaseCalendar;

import java.util.*;

import java.text.SimpleDateFormat;

@Service("NewsCommentService")
public class NewsCommentImpl implements NewsCommentService {
    public int findNewsIdByTitle(String title){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper =
                sqlSession.getMapper(NewsCommentMapper.class);
        int newsId = newsCommentMapper.getTextidByTextName(title);
        sqlSession.close();
        return newsId;
    }
    public void putCommentToDB(int textid,int userid,String comment){
        // 获取当前时间
        Date currentTime = new Date();
        // 创建一个SimpleDateFormat对象，用于格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将当前时间转换为字符串
        String timeString = sdf.format(currentTime);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper = sqlSession.getMapper(NewsCommentMapper.class);
        newsCommentMapper.pressComment(textid,userid,comment,timeString);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("新增"+textid+"号文章的"+userid+"号用户的在"+timeString+"发布的"+comment+"评论");
    }
    public String findCommentByNewsId(int newsid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        NewsCommentMapper newsCommentMapper = sqlSession.getMapper(NewsCommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Comment> comments = newsCommentMapper.getCommentByTextid(newsid);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Comment comment : comments) {
            int userId = comment.getUserId();
            User user = userMapper.getUserByUid(userId);
            System.out.println(user);
            String nickname = user.getNickname();
            String avatar = user.getAvatarurl();
            String createTime = comment.getCreateTime();
            String content = comment.getComment();
            Map<String, Object> data = new HashMap<>();
            data.put("comment_time", comment.getCreateTime());
            data.put("comment_nick",nickname);
            data.put("comment_content", content);
            data.put("comment_avatar",avatar);
            System.out.println(comment.getCreateTime()+" "+user.getNickname()+" "+content+" "+user.getAvatarurl());
            result.add(data);
            // 在这里处理userId, createTime和content
        }
        JSONObject map = new JSONObject();
        map.put("total",result.size());
        map.put("rows",result);
        map.put("code",200);
        map.put("msg","查询成功");
        sqlSession.close();
        return map.toString();
    }


    public void putVideoCommentToDB(int videoid,int userid,String comment){
        // 获取当前时间
        Date currentTime = new Date();
        // 创建一个SimpleDateFormat对象，用于格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将当前时间转换为字符串
        String timeString = sdf.format(currentTime);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        VideoCommentMapper newsCommentMapper = sqlSession.getMapper(VideoCommentMapper.class);
        newsCommentMapper.pressComment(videoid,userid,comment,timeString);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("新增"+videoid+"号文章的"+userid+"号用户的在"+timeString+"发布的"+comment+"评论");
    }
    public String findCommentByVideoId(int videoid){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        VideoCommentMapper newsCommentMapper = sqlSession.getMapper(VideoCommentMapper.class);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<Comment> comments = newsCommentMapper.getCommentByVideoid(videoid);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Comment comment : comments) {
            int userId = comment.getUserId();
            User user = userMapper.getUserByUid(userId);
            System.out.println(user);
            String nickname = user.getNickname();
            String avatar = user.getAvatarurl();
            String createTime = comment.getCreateTime();
            String content = comment.getComment();
            Map<String, Object> data = new HashMap<>();
            data.put("comment_time", comment.getCreateTime());
            data.put("comment_nick",nickname);
            data.put("comment_content", content);
            data.put("comment_avatar",avatar);
            System.out.println(comment.getCreateTime()+" "+user.getNickname()+" "+content+" "+user.getAvatarurl());
            result.add(data);
            // 在这里处理userId, createTime和content
        }
        JSONObject map = new JSONObject();
        map.put("total",result.size());
        map.put("rows",result);
        map.put("code",200);
        map.put("msg","查询成功");
        sqlSession.close();
        return map.toString();
    }

}
